package org.kgduyjj.toolkit.enumtype;

import java.util.concurrent.ThreadLocalRandom;

import org.kgduyjj.toolkit.Asserts;

/**
 * 生成的随机数类型
 *
 * @author Kgduyjj
 */
public enum RandomSource {
    /** 全数字型 */
    NUMBER("0123456789"),
    /** 全字母型 */
    LETTERS_FULL("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"),
    /** 大写字母型 */
    LETTERS_UPPERCASE("ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
    /** 小写字母型 */
    LETTERS_LOWERCASE("abcdefghijklmnopqrstuvwxyz"),
    /** 全数字及全字母型 */
    ALL("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");

    protected static final int DEFAULT_COUNT = 6;
    /**
     * 随机数资源字符串
     */
    protected String source;

    RandomSource(String source) {
        this.source = source;
    }

    /**
     * 获取随机数字的元数据
     * 
     * @return
     */
    public String getSource() {
        return source;
    }

    /**
     * 生成指定位数的随机字符
     * 
     * @param count
     * @return
     */
    public String generate(int count) {
        if (0 == count) {
            return "";
        }
        Asserts.notTrue(count > 0, "Requested random string length {} is less than 0.", count);
        final ThreadLocalRandom random = ThreadLocalRandom.current();
        char[] buffer = new char[count];
        for (int i = 0; i < count; i++) {
            buffer[i] = this.getSource().charAt(random.nextInt(this.getSource().length()));
        }
        return new String(buffer);
    }

    /**
     * 生成6位的随机字符
     * 
     * @return
     */
    public String generate() {
        return generate(DEFAULT_COUNT);
    }
}
